package com.bluetooth.heartchecked.adapter;


import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.bluetooth.heartchecked.R;

import java.util.List;

public class DevicesAdapter extends BaseAdapter {

    Context context;
    List<BluetoothDevice> devices;
    LayoutInflater inflater;
    OnConnectClickListener listener;

    public DevicesAdapter(Context context, List<BluetoothDevice> devices) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.devices = devices;
    }

    public void setOnConnectClickListener(OnConnectClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return devices.size();
    }

    @Override
    public Object getItem(int position) {
        return devices.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewGroup vg;

        if (convertView != null) {
            vg = (ViewGroup) convertView;
        } else {
            vg = (ViewGroup) inflater.inflate(R.layout.item_device, null);
        }

        BluetoothDevice device = devices.get(position);

        /*******名称********/
        TextView tvName = vg.findViewById(R.id.name);
        tvName.setText(device.getName());

        /*******地址********/
        TextView tvAddress = vg.findViewById(R.id.address);
        tvAddress.setText(device.getAddress());

        /*******配对状态********/
        TextView tvBondState = vg.findViewById(R.id.bond_state);
//        BluetoothDevice.BOND_NONE//未配对
//        BluetoothDevice.BOND_BONDED//配对成功
//        BluetoothDevice.BOND_BONDING//正在配对
        switch (device.getBondState()) {
            case BluetoothDevice.BOND_NONE:
                tvBondState.setText("未配对");
                break;
            case BluetoothDevice.BOND_BONDED:
                tvBondState.setText("配对成功");
                break;
            case BluetoothDevice.BOND_BONDING:
                tvBondState.setText("正在配对");
                break;
            default:
                tvBondState.setText("未配对");
                break;
        }

        /*******连接状态********/
        TextView tvConnectStatus = vg.findViewById(R.id.connect_status);
        switch (device.getBondState()) {
            case BluetoothDevice.BOND_NONE:
                tvConnectStatus.setText("可连接");
                break;
            case BluetoothDevice.BOND_BONDED:
            case BluetoothDevice.BOND_BONDING:
                tvConnectStatus.setText("不可连接");
                break;
            default:
                tvConnectStatus.setText("可连接");
                break;
        }

        /*******连接按钮********/
        Button btnConnect = vg.findViewById(R.id.connect);
        switch (device.getBondState()) {
            case BluetoothDevice.BOND_NONE:
                btnConnect.setVisibility(View.VISIBLE);
                break;
            case BluetoothDevice.BOND_BONDED:
            case BluetoothDevice.BOND_BONDING:
                btnConnect.setVisibility(View.GONE);
                break;
            default:
                btnConnect.setVisibility(View.GONE);
                break;
        }

        btnConnect.setTag(position);
        btnConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onConnectClick((int)view.getTag());
                }
            }
        });

        return vg;
    }

    public interface OnConnectClickListener {

        void onConnectClick(int position);
    }
}
